﻿using Avalonia;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace mesign.pc.Helper
{
    public class ImageHelper
    {
        /// <summary>
        /// 图标缓存，不然每次创建的不会释放，导致程序占用内存过大
        /// </summary>
        public static Dictionary<string, Bitmap> DicImage = new Dictionary<string, Bitmap>();
        public static IBitmap GetBitmap(string name, string folderName = "/Assets/icon")
        {
            try
            {
                if (string.IsNullOrEmpty(name))
                    return null;
                string assemblyName = Assembly.GetEntryAssembly().GetName().Name;
                string key = $"avares://{assemblyName}{folderName}/{name}";
                if (DicImage.ContainsKey(key))
                {
                    return DicImage[key];
                }
                var uri = new Uri(key);
                var assets = AvaloniaLocator.Current.GetService<IAssetLoader>();
                var stream = assets.Open(uri);
                var bitMap = new Bitmap(stream);
                DicImage.Add(key, bitMap);
                return bitMap;
            }
            catch (Exception ex)
            {
                //文件未找到
            }
            return null;
        }

        public static Stream GetStream(string name, string folderName = "/Assets/icon")
        {
            try
            {
                if (string.IsNullOrEmpty(name))
                    return null;
                string assemblyName = Assembly.GetEntryAssembly().GetName().Name;
                var uri = new Uri($"avares://{assemblyName}{folderName}/{name}");
                var assets = AvaloniaLocator.Current.GetService<IAssetLoader>();
                var stream = assets.Open(uri);
                return stream;
            }
            catch (Exception ex)
            {
                //文件未找到
            }
            return null;
        }

        /// <summary>
        /// 获取全名
        /// </summary>
        /// <param name="name"></param>
        /// <param name="folderName"></param>
        /// <returns></returns>
        public static string GetFullName(string name, string folderName = "/Assets/icon")
        {
            if (string.IsNullOrEmpty(name))
                return null;
            string assemblyName = Assembly.GetEntryAssembly().GetName().Name;
            var uri = $"avares://{assemblyName}{folderName}/{name}";
            return uri;
        }

       

        /// <summary>
        /// 获取文件扩展名
        /// </summary>
        /// <param name="fileName">文件名（包含扩展名）</param>
        /// <returns></returns>
        public static string GetFileExtension(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                return "";
            }
            else
            {
                if (fileName.LastIndexOf(".") != -1 && fileName.LastIndexOf(".") != 0)
                {
                    return fileName.Substring(fileName.LastIndexOf(".") + 1).ToLowerInvariant();
                }
                else
                {
                    return "";
                }
            }
        }

       

      
    }

}
